//
//  TFSignInView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/10.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFSignInView: UIView {
    @objc let round = UIButton()
    @objc let close = UIButton()
    //签到ID
    @objc var signId:String = ""
    //签到
    @objc let SignInLaber = UILabel()
    @objc let timeLabel = UILabel()
    //总时长
    @objc var duration:NSInteger = 0
    //定时器
    @objc var timer:Timer?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.round.backgroundColor = UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 0.5)
        self.addSubview(self.round)
        
        self.SignInLaber.text = "签到"
        self.SignInLaber.numberOfLines = 0
        self.SignInLaber.adjustsFontSizeToFitWidth = true
        self.SignInLaber.minimumScaleFactor = 10
        self.SignInLaber.textAlignment =  .center
        self.SignInLaber.clipsToBounds = true

        self.SignInLaber.backgroundColor =  UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)
        self.SignInLaber.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        self.SignInLaber.font = UIFont.PingFang(size: 25)
        self.addSubview(self.SignInLaber)
        
        self.timeLabel.numberOfLines = 0
        self.addSubview(self.timeLabel)
        
        self.timeLabel.font = UIFont.PingFang(size: 15)
        self.timeLabel.text = "00:00"
        self.timeLabel.textAlignment =  .center
        self.timeLabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        self.close.setImage(UIImage.init(named: "HTLive.bundle/close"), for: .normal)
        self.close.addTarget(self, action: #selector(closeClick), for: .touchUpInside)
        self.addSubview(self.close)
    }
    
    @objc func closeClick()
    {
        
        let alert = UIAlertController.init(title: "提示", message: "是否结束本次点名?", preferredStyle:.alert)
        let cancel:UIAlertAction = UIAlertAction.init(title: "取消", style: .default) { action in
            
        }
        alert.addAction(cancel)
        let otherAction:UIAlertAction = UIAlertAction.init(title: "确定", style: .default) { action in
            self.endTimeLaer()
        }
        alert.addAction(otherAction)
        
        let window = TalkfunSharedTools.keyWindow()
       
            if  UIDevice.current.model.hasPrefix("iPad") {
                alert.popoverPresentationController?.sourceView = window
                alert.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: 1.0, height: 1.0)
            }
           
            window.rootViewController!.present(alert, animated: true)
        
   
    }
   @objc func countdown()
    {
        let duration = self.duration
   
   
       let text = TalkfunSharedTools.minutesSndSeconds(duration)
        
        if (self.duration==0) {
            self.endTimeLaer()
       
        }
        self.timeLabel.text = text as String
        if (self.timer==nil) {
            // 创建NSTimer对象
            self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)

            RunLoop.main.add(self.timer! , forMode: RunLoop.Mode.default)
           
        }
    }
    @objc func timerAction()
    {
        self.duration = self.duration-1
        self.countdown()
    }
    @objc func endTimeLaer()
    {
        self.isHidden = true
        if self.timer != nil {
            self.timer?.invalidate()
            self.timer = nil
        }
      
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.close.frame = CGRect(x: self.frame.size.width-28, y: 0, width: 28, height: 28)
    
        self.round.frame = CGRect(x: (self.frame.size.width - 90)/2, y: 17, width: 90, height: 90)
    
        self.round.layer.cornerRadius = self.round.bounds.size.width / 2
         
        self.SignInLaber.frame = CGRect(x: (self.frame.size.width - 80)/2, y: 17+5, width: 80, height: 80)

        self.SignInLaber.layer.cornerRadius = self.SignInLaber.bounds.size.width / 2
         
         self.timeLabel.frame = CGRect(x: (self.frame.size.width - 40)/2, y: self.frame.size.height - 21 - 16, width: 40, height: 21)
      
    }
    
    @objc func upaDataLanguage()
    {
        self.SignInLaber.text = "签到"
    }

    
}
